package com.meituan.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class LoggerConfig {


    private static final Logger logger = LoggerFactory.getLogger(LoggerConfig.class);


    @Pointcut(value = "execution(* *.*.service.impl.*.*(..))")
    public void pt1(){};


    @Around(value = "pt1()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        logger.debug("==================开始记录日志================="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        MethodSignature signature = (MethodSignature)proceedingJoinPoint.getSignature();
        logger.debug("当前执行的方法为：{}"+signature.getMethod().getName());
        Object proceed =null;
        try {
            proceed = proceedingJoinPoint.proceed();
            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            logger.error("方法异常：{}"+throwable.getMessage());
        }finally {
            logger.debug("==================结束记录日志================="+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            return proceed;
        }

    }
}
